package controller;

import java.io.IOException;
//import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
import manager.UserManager;
//import data.dao.LeaveDAOException;
import data.dao.UserDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dao.daoImplementation.SQLException;
//import data.dao.daoImplementation.NotFoundException;
import data.dto.UserDTO;
import data.dto.userTypeDTO;

/**
 * Servlet implementation class LeaveEntitlement
 */
@WebServlet("/LeaveEntitlement")
public class LeaveEntitlement extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LeaveEntitlement() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (!util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			String type = request.getParameter("type") == null ? "" : request
					.getParameter("type").trim();
			page = "/admin/ViewPage.jsp?type=entitlement";

			userTypeDTO ut = null;
			UserManager um = new UserManager();

			if (type.equalsIgnoreCase("delete")) {
				String leavetype = request.getParameter("id");

				Integer BI = new Integer(leavetype);
				short id = BI.shortValue();

				ut = new userTypeDTO();
				ut.setId(id);
				try {
					um.deleteUserType(ut);
				} catch (UserDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			if (type.equalsIgnoreCase("update")) {
				ut = new userTypeDTO();
				ut.setId(Short.parseShort(request.getParameter("id")));
				ut.setName(request.getParameter("name"));
				ut.setMedicalLeave(Short.parseShort(request
						.getParameter("medicalLeave")));
				ut.setAnnualLeave(Short.parseShort(request
						.getParameter("annualLeave")));
				ut.setCompensationLeave(Float.parseFloat(request
						.getParameter("compensationLeave")));
				ut.setStatus((short) (1));
				try {

					um.updateUserType(ut);

				} catch (UserDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (type.equalsIgnoreCase("insert")) {
				ut = new userTypeDTO();
				ut.setId(Short.parseShort(request.getParameter("id")));
				ut.setName(request.getParameter("name"));
				ut.setMedicalLeave(Short.parseShort(request
						.getParameter("medicalLeave")));
				ut.setAnnualLeave(Short.parseShort(request
						.getParameter("annualLeave")));
				ut.setCompensationLeave(Float.parseFloat(request
						.getParameter("compensationLeave")));
				ut.setStatus((short) (1));

				try {

					um.insertUserType(ut);

				} catch (UserDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			try {
				request.setAttribute("leaveEntitlement", um.findAllUserType());
			} catch (UserDAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);
	}
}
